﻿/*
 * 由SharpDevelop创建。
 * 用户： Administrator
 * 日期: 2024/12/1
 * 时间: 16:04
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.IO;
using System.Net;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace hitokoto
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			string inipath=Directory.GetCurrentDirectory()+"\\setting.ini"; 
			int x=int.Parse(iniHelper.Read("hitokoto","left",inipath));
			int y=int.Parse(iniHelper.Read("hitokoto","top",inipath));
			this.Location=new Point(x,y);
			 
			int w=int.Parse(iniHelper.Read("hitokoto","width",inipath));
			int h=int.Parse(iniHelper.Read("hitokoto","height",inipath));
			this.Size=new Size(w,h);
			
			label1.Text="叶樱";
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		void Timer1Tick(object sender, EventArgs e)
		{
			string inipath=Directory.GetCurrentDirectory()+"\\setting.ini"; 
			string color=iniHelper.Read("Topbar","backcolor",inipath);
			this.BackColor =ColorTranslator.FromHtml(color); 
			
			int x=int.Parse(iniHelper.Read("hitokoto","left",inipath));
			this.Left=x;
		}
		void Label1Click(object sender, EventArgs e)
		{
			string inipath=Directory.GetCurrentDirectory()+"\\setting.ini"; 
			string type=iniHelper.Read("hitokoto","type",inipath);
			switch (type) {
				case "1":
				label1.Text=GetWebClient("https://v1.hitokoto.cn/?encode=text");
			//  label1.Width= TextRenderer.MeasureText(label1.Text, label1.Font).Width; 
			//	label1.Location=(Point)new Size((Screen.PrimaryScreen.Bounds.Width-label1.Width)/2, this.Height/4);
				break;
				
				case "2":
				string txt=iniHelper.Read("hitokoto","txt",inipath);
				string[] line=txt.Split('|');
				Random rd = new Random();
	            int i = rd.Next(0,line.Length);
	            label1.Text=line[i];
	         //   label1.Width= TextRenderer.MeasureText(label1.Text, label1.Font).Width; 
			//	label1.Location=(Point)new Size((Screen.PrimaryScreen.Bounds.Width-label1.Width)/2, this.Height/4);
				break;
			}
		}
		private string GetWebClient(string url)
		{
        	ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
		    
		    string strHTML = "";
		    WebClient myWebClient = new WebClient();
		    Stream myStream = myWebClient.OpenRead(url);
		    StreamReader sr = new StreamReader(myStream, System.Text.Encoding.GetEncoding("utf-8"));
		    strHTML = sr.ReadToEnd();
		    myStream.Close();
		    return strHTML;
		}
	}
}
